package com.uetoken.cn.base;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.uetoken.cn.R;
import com.uetoken.cn.language.MultiLanguageUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * @author 谢杨
 * @time 2018/6/19 10:48
 * @class 描述:Activity基类
 */
public abstract class BaseActivity extends AppCompatActivity {
    private QMUITipDialog qmuiTipDialog;

    /**
     * 获取布局文件
     *
     * @return 布局id
     */
    public abstract int getLayoutId();

    /**
     * 初始化控件
     */
    public abstract void initView();

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 初始化数据
     */
    public abstract void init();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        setContentView(getLayoutId());
        initView();
        initData();
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(MultiLanguageUtil.attachBaseContext(newBase));

    }

    public void showLoading() {
        try {
            if (qmuiTipDialog == null) {
                qmuiTipDialog = new QMUITipDialog.Builder(this)
                        .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                        .setTipWord(getString(R.string.str_dialog_loading))
                        .create();
            }
            if (qmuiTipDialog.isShowing()) {
                return;
            }
            Context context = ((ContextWrapper) qmuiTipDialog.getContext()).getBaseContext();
            if (ActivityUtils.isActivityAlive(context)) {
                qmuiTipDialog.show();
            } else {
                qmuiTipDialog = null;
            }
        } catch (Exception e) {
            this.qmuiTipDialog = null;
        }
    }

    public void dissmissDialog() {
        try {
            if (qmuiTipDialog != null) {
                if (qmuiTipDialog.isShowing()) { //check if dialog is showing.

                    //get the Context object that was used to great the dialog
                    Context context = ((ContextWrapper) qmuiTipDialog.getContext()).getBaseContext();

                    //if the Context used here was an activity AND it hasn't been finished or destroyed
                    //then dismiss it
                    if (ActivityUtils.isActivityAlive(context)) {
                        qmuiTipDialog.dismiss();
                    } else {
                        qmuiTipDialog = null;
                    }
                }

            }
        } catch (Exception e) {
            this.qmuiTipDialog = null;
        }
    }

    @Override
    public void finish() {
        dissmissDialog();
        super.finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this); // 基础指标统计，不能遗漏
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this); // 基础指标统计，不能遗漏
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    /** here */
                    v.clearFocus();
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}
